﻿using System;

namespace DataAccessLayer
{
    /// <summary>
    /// Class that contains description of employee
    /// </summary>
    public class User
    {
        /// <summary>
        /// user id
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// Last name of employee
        /// </summary>
        public string LastName { get; set; }

        /// <summary>
        /// First name of employee
        /// </summary>
        public string FirstName { get; set; }

        /// <summary>
        /// Login of employee
        /// </summary>
        private string login;
        public string Login
        {
            get { return login; }
        }
        /// <summary>
        /// Hashed password of employee
        /// </summary>
        private string passwordHash;
        public string Password
        {
            get { return passwordHash; }
        }
        /// <summary>
        /// user's responsibilieties for using system
        /// </summary>
        private readonly DataBaseRoles accessRole;

        public DataBaseRoles AccessRole
        {
            get { return accessRole; }
        }

        public Roles Role { get; set; }

        /// <summary>
        /// constructor of User with parametres
        /// </summary>
        /// <param name="lastName">User's sirname</param>
        /// <param name="firstName">User's name</param>
        /// <param name="login">User's login</param>
        /// <param name="password">user's password (hashed if user is signed in)</param>
        /// <param name="role">user's rights</param>
        /// <param name="newUser">by default is false and means that user is signed in</param>
        public User(int id, string lastName, string firstName, string login, string password, int role, bool newUser = false)
        {
           
            // checks if exist empty data fields
            
            if (!String.IsNullOrWhiteSpace(lastName) && !String.IsNullOrWhiteSpace(firstName) &&
                !String.IsNullOrWhiteSpace(login) && !String.IsNullOrWhiteSpace(password))
            {
                Id = id;
                LastName = lastName;
                FirstName = firstName;
                this.login = login;
                passwordHash = newUser ? SimpleHash.ComputeHash(password, null) : password;
                Role = (Roles) role;
            }
        }
        /// <summary>
        /// This method  changes existing user's password
        /// to another one. It takes two parametres(old password and new password)
        /// and returns true or false if this operation successed or not.
        /// </summary>
        /// <param name="oldPassword">specify the user's password that shoud be changed</param>
        /// <param name="newPassword">specify the user's password that will renew the old one</param>
        /// <returns>bool value</returns>
        public bool PasswordChange(string oldPassword, string newPassword)
        {
            if (IsPasswordValid(oldPassword, Password))
            {
                passwordHash = SimpleHash.ComputeHash(newPassword, null);
                return true;
            }
             return false;
        }

        public string FullName()
        {
            return String.Format(LastName+" "+FirstName);
        }

        /// <summary>
        /// This method checkes password validation. It compares two hashed passwords 
        /// </summary>
        /// <param name="password">specify the password that user entered in textbox</param>
        /// <param name="passwordHash">specify the user's hashed password from data table</param>
        /// <returns>bool value</returns>
        public static bool IsPasswordValid(string password, string passwordHash)
        {
            return SimpleHash.VerifyHash(password, passwordHash);
        }

        /// <summary>
        /// Overrided method ToString that describes user
        /// </summary>
        /// <returns>String with user description</returns>
        public override string ToString()
        {
            return String.Format("LastName: {0}\nFirstName: {1}\nLogin: {2}\nPasswordHash: {3}\nDB Role: {4}", LastName, FirstName,
                Login, Password, AccessRole.ToString());
        }
    }
}
